package ac.manchester.cs.afzal.term_classifier.information_retrieval;

import ac.manchester.cs.afzal.term_classifier.miscellaneous.File_Manager_Impl;
import ac.manchester.cs.afzal.term_classifier.nlp.String_Manipulator;
import ac.manchester.cs.afzal.term_classifier.nlp.String_Manipulator_Impl;

import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.File;
import java.io.FileInputStream;

/**
 * Title: Term Classifier
 *
 * Description: A toolkit to find the relevance of candidate terms from a text corpus
 * to any particular topic/sub-domain using a term classification driven approach. The
 * system utilizes the lexical and contextual profiles of the candidate and domain-representing
 * "Resource Terms" (Seed and Ontological). The lexical and contextual profiles of candidate terms
 * are compared with those of Resource Terms to measure their relevance to the topic of interest.
 *
 * Parse_PMC_XML class
 * <p>Company: The University of Manchester</p>
 *
 * @author Hammad Afzal
 * @version 1.0
 *
 */
public class Parse_PMC_XML
{

    private String CORPUS_XML = "XML_CORPUS_DIR";
    private String CORPUS_TEXT = "DOCUMENTS_CORPUS";

    private final String EXPRESSIONS_TO_BE_REMOVED[] =
    {
        "<sec>", "</sec>", "<st>", "</st>", "<it>", "</it>", "<abbrgrp>",
        "</abbrgrp>", "&#8226;", "&#160;",
        "<abbr bid=\"(.){0,3}\">(.){0,3}</abbr>",
        "<figr fid=\"(.){0,3}\">(.){0,7}</figr>",
        "<tblr tid=\"(.){0,3}\">(.){0,7}</tblr>",
        "<supplr sid=\"(.){0,3}\">(.){0,7}</supplr>"
    };
    private final String TAGS_TO_REPLACE_WITH_NEWLINE[] =
    {
        "<p>", "</p>"
    };
    private final String TAGS_TO_REPLACE_WITH_SPACE[] =
    {
        "</title>", "</p>", "</sec>", "<sup>", "</sup>", "<sub>", "</sub>",
        "   "
    };
    
    File_Manager_Impl fileManager;
    String_Manipulator strMan = new String_Manipulator_Impl();
    Properties props;

    public Parse_PMC_XML()
    {
        try
        {
            props = new Properties();
            props.load(new FileInputStream("Term_Classification.properties"));
        }
        catch (IOException ex)
        {
            Logger.getLogger(Parse_PMC_XML.class.getName()).log(Level.SEVERE, null, ex);
        }
        CORPUS_XML = props.getProperty(CORPUS_XML);
        CORPUS_TEXT = props.getProperty(CORPUS_TEXT);
    }

    /**
     * Converts the XML files of PubMedCentral XML format to text
     */
    public void convert_pmc_xml_to_text()
    {
        File dir = new File(CORPUS_XML);
        String[] str = dir.list();

        File xmlFile;

        for(String inputFileName : str)
        {
            xmlFile = new File(CORPUS_XML + inputFileName);
            String outputFileName = fileManager.strip_file_name_from_file_name_plus_extension(inputFileName, "xml");
            outputFileName = outputFileName + ".txt";

            StringBuffer contents = new StringBuffer();
            StringBuffer parsed_contents = new StringBuffer();

            contents = fileManager.fileReader(xmlFile);

            /*============================== Extracting Titles =========================*/
            String title = strMan.string_extractor(contents.toString(), "<title>", "</title>");
            parsed_contents.append(title);
            parsed_contents.append(System.getProperty("line.separator"));

            /*============================== End Extracting Titles =========================*/

            /*============================== Extracting PubmedID =========================*/
            String pid = strMan.string_extractor(contents.toString(), "idtype=\"pmpid\">", "</pubid>");
            /*============================== End Extracting PubmedID =========================*/

            /*============================== Extracting Abstracts =========================*/
            int index = 0;
            int temp = 0;

            index = contents.indexOf("<abs>", index);
            index = index + 153;
            temp = index;
            index = contents.indexOf("</abs>", index);

            if(index != -1)
            {
                String abstract_ = contents.substring(temp, index);
                abstract_ = remove_references(abstract_);
                abstract_ = extract_heading(abstract_);
                parsed_contents.append(abstract_);
            }

            /*============================== End Extracting Abstracts =========================*/

            String trimmed_text = strMan.string_extractor(contents.toString(), "<bdy>", "</bdy>");

            trimmed_text = remove_references(trimmed_text);
            trimmed_text = extract_heading(trimmed_text);

            parsed_contents.append(trimmed_text);

            /*============================== Writing a Parsed File =========================*/
            fileManager.fileWriter(parsed_contents, CORPUS_TEXT + outputFileName);
        }

    }

    /*
     * It removes the expressions from the text to clean it. The removed expressions include
     * the equations, tables etc.
     */
    public String remove_references(String str)
    {

        for(String patternStr : EXPRESSIONS_TO_BE_REMOVED)
        {
            str = strMan.pattern_replacer(str, patternStr, "");
        }
        return str;
    }

    /*
     * 
     */
    public String extract_heading(String str)
    {
        for(String patternStr : TAGS_TO_REPLACE_WITH_NEWLINE)
        {
            str = strMan.pattern_replacer(str, patternStr, "\n");
        }

        for(String patternStr : TAGS_TO_REPLACE_WITH_SPACE)
        {
            str = strMan.pattern_replacer(str, patternStr, "");
        }

        str = strMan.string_replacer(str, "<url>", "</url", "");
        str = strMan.string_replacer(str, "<abbr bid", "</abbr>", "");
        str = strMan.string_replacer(str, "<inline-formula>", "</inline-formula>", "");
        str = strMan.string_replacer(str, "<display-formula", "</display-formula>", "");
        str = strMan.string_replacer(str, "<m:math", "</m:math>", "");
        str = strMan.string_replacer(str, "<graphic file", "/>", "");
        str = strMan.string_replacer_start_zero(str, "<fig id=", "</fig>", "");
        str = strMan.string_replacer_start_zero(str, "<tbl id=", "</tbl>", "");
        str = strMan.string_replacer_start_zero(str, "<suppl id=", "</suppl>", "");

        return str;
    }
}
